package com.dao;

import com.utils.JDBCUnits;
import com.utils.MD5;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDao {
    /**
     * 根据信封的id获取用户的id
     * @param envelope_id
     * @return 用户的id
     */
    public static int getID(int envelope_id){
        Connection c = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            c = JDBCUnits.getConnectino();
            String sql = "select * from envelope where envelope_id = ?";
            ps = c.prepareStatement(sql);
            ps.setInt(1,envelope_id);
            rs = ps.executeQuery();
            if(rs.next()){
                return rs.getInt("user_id");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUnits.close(c,ps,rs);
        }
        return 0;
    }

    /**
     * 根据用户的名字获取用户的信息
     * @param name
     * @return 用户的id
     */
    public static int getUser_id(String name){
        Connection c = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            c = JDBCUnits.getConnectino();
            String sql = "select * from t_usr where name = ?";
            ps = c.prepareStatement(sql);
            ps.setString(1,name);
            rs = ps.executeQuery();
            if(rs.next()){
                return rs.getInt("id");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUnits.close(c,ps,rs);
        }
        return 0;
    }

    public static void updateName(String name , int id){
        Connection c = null;
        PreparedStatement ps = null;
        try {
            c = JDBCUnits.getConnectino();
            String sql = "update t_usr set name=? where id =?";
            ps = c.prepareStatement(sql);
            ps.setString(1,name);
            ps.setInt(2,id);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUnits.close(c,ps);
        }
    }

    public static void updatePassword(int id ,String password){
        Connection c = null;
        PreparedStatement ps = null;
        try {
            c = JDBCUnits.getConnectino();
            String sql = "update t_usr set password=? where id =?";
            ps = c.prepareStatement(sql);
            ps.setString(1, MD5.getMD5String(password));
            ps.setInt(2,id);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUnits.close(c,ps);
        }
    }

    public static List<Integer> likeNote(int id){
        List<Integer> myLike = new ArrayList<>();
        Connection c = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            c = JDBCUnits.getConnectino();
            String sql = "select * from likesystem where user_id = ?";
            ps = c.prepareStatement(sql);
            ps.setInt(1,id);
            rs = ps.executeQuery();
            while(rs.next()){
                myLike.add(rs.getInt("note_id"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUnits.close(c,ps,rs);
        }

        return myLike;
    }
}
